////////////////////////////////////////////////////////////////////////////////
//                                                                            //
// The MIT License (MIT)                                                      //
//                                                                            //
// Copyright (c) 2013 Charles Teague                                          //
//                                                                            //
// Permission is hereby granted, free of charge, to any person obtaining a    //
// copy of this software and associated documentation files (the "Software"), //
// to deal in the Software without restriction, including without limitation  //
// the rights to use, copy, modify, merge, publish, distribute, sublicense,   //
// and/or sell copies of the Software, and to permit persons to whom the      //
// Software is furnished to do so, subject to the following conditions:       //
//                                                                            //
// The above copyright notice and this permission notice shall be included in //
// all copies or substantial portions of the Software.                        //
//                                                                            //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   //
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    //
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    //
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        //
// DEALINGS IN THE SOFTWARE.                                                  //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using RPSDAL;
using RPSDAL.EntityClasses;
using RPSDAL.CollectionClasses;
using RPSDAL.HelperClasses;

using SD.LLBLGen.Pro.ORMSupportClasses;


public partial class CallFinishedPageField : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Parameters.LoggedUser != -1) 
            {
                UserEntity eUser = new UserEntity(Parameters.LoggedUser);
                RoleEntity eRole = new RoleEntity(eUser.RoleId);

                if (!eRole.HasConfigureCallFinishedPage)
                {
                    Parameters.LoggedUser = -1;
                    Response.Redirect("Default.aspx");
                    return;
                }
            }

            cmbFieldType.Attributes.Add("OnChange", "FieldTypeChange(this);");
            btnAddValue.Attributes.Add("OnClick", "return AddValue();");
            btnDelete.Attributes.Add("OnClick", "return RemoveValue()");

            int callFinishedPageFieldId = 0;
            if (!String.IsNullOrEmpty(Request.QueryString["id"]) && Int32.TryParse(Global.DecryptString(Request.QueryString["id"].ToString()),out callFinishedPageFieldId))
            {
                CallFinishedPageFieldEntity eCallFinishedPageField = new CallFinishedPageFieldEntity(callFinishedPageFieldId);

                txtName.Text = eCallFinishedPageField.Name;
                cmbFieldType.SelectedValue = eCallFinishedPageField.Type.ToString();

                if (eCallFinishedPageField.Type == Parameters.COMBOBOBOX_TYPE) 
                {
                    CallFinishedDropdownFieldValueCollection cCallFinishedDropdownFieldValues = new CallFinishedDropdownFieldValueCollection();
                    IPredicateExpression filter = new PredicateExpression();
                    filter.Add(CallFinishedDropdownFieldValueFields.CallPageFinishedFieldId == eCallFinishedPageField.CallPageFinishedFieldId);
                    cCallFinishedDropdownFieldValues.GetMulti(filter);

                    lstComboboxValues.DataSource = cCallFinishedDropdownFieldValues;
                    lstComboboxValues.DataTextField = CallFinishedDropdownFieldValueFields.Value.Name;
                    lstComboboxValues.DataValueField = CallFinishedDropdownFieldValueFields.Value.Name;
                    lstComboboxValues.DataBind();

                    for (int i = 0; i < cCallFinishedDropdownFieldValues.Count; i++) 
                    {
                        txtComboValues.Value += cCallFinishedDropdownFieldValues[i].Value + ";";
                    }

                    comboboxValuesId.Style["display"] = "inline";
                }

            }
        }
    }

    protected void Save(object sender, EventArgs e)
    {
        Global.UserLogSave(Parameters.LoggedUser, "Save Filed Call Finished Page");
        CallFinishedPageFieldEntity eCallFinishedPageField = new CallFinishedPageFieldEntity();

        if (Page.IsPostBack && !String.IsNullOrEmpty(Request.QueryString["id"])) 
        {
            eCallFinishedPageField = new CallFinishedPageFieldEntity(Convert.ToInt32(Global.DecryptString(Request.QueryString["id"].ToString())));
        }

        CallFinishedPageFieldCollection cCallFinishedPageFields = new CallFinishedPageFieldCollection();
        cCallFinishedPageFields.GetMulti(null,0,
            new SortExpression(new SortClause(CallFinishedPageFieldFields.CallPageFinishedFieldId,SortOperator.Descending)));

        eCallFinishedPageField.Name = txtName.Text;
        eCallFinishedPageField.Type = Convert.ToInt32(cmbFieldType.SelectedValue);
        eCallFinishedPageField.OrderIndex = cCallFinishedPageFields.Count==0?1:(cCallFinishedPageFields[0].CallPageFinishedFieldId + 1);

        eCallFinishedPageField.Save();

        CallFinishedDropdownFieldValueCollection cCallFinishedDropdownFieldValues = new CallFinishedDropdownFieldValueCollection();
        IPredicateExpression filter = new PredicateExpression();
        filter.Add(CallFinishedDropdownFieldValueFields.CallPageFinishedFieldId == eCallFinishedPageField.CallPageFinishedFieldId);
        cCallFinishedDropdownFieldValues.DeleteMulti(filter);

        string[] comboValues = txtComboValues.Value.Split(';');
        for (int i = 0; i<comboValues.Length-1; i++) 
        {
            CallFinishedDropdownFieldValueEntity eCallFinishedDropdownFieldValue = new CallFinishedDropdownFieldValueEntity();
            eCallFinishedDropdownFieldValue.CallPageFinishedFieldId = eCallFinishedPageField.CallPageFinishedFieldId;
            eCallFinishedDropdownFieldValue.Value = comboValues[i];
            eCallFinishedDropdownFieldValue.Save();
        }

        Response.Redirect("CallFinishedPageField.aspx?id=" + Global.EncryptString(eCallFinishedPageField.CallPageFinishedFieldId.ToString()));
    }


    protected void Cancel(object sender, EventArgs e)
    {
        Response.Redirect("CallFinishedPageAdministration.aspx");
    }


}
